<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceJinModel;
use app\index\model\DeviceTuiListModel;
use app\index\model\DeviceTuiModel;
use app\index\model\DeviceYunListModel;
use app\index\model\DeviceYunModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\service\BianhaoService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Device extends Common
{
    public function detail_device_jin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jin.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['list.shenum','like', '%'.$param['listshenum'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jin.status','=', $param['status']];
        }


        $data['data'] = DeviceJinListModel::alias('list')
            ->field('jin.*,e.name as enginname,bill.sericnum,cai.name as cainame,cai.gonger
            ,list.id as listid,list.name as listname,list.unit as listunit,list.price as listprice,list.digit as listdigit
            ,list.jindt as listjindt,list.shenum as listshenum,list.explain as listexplain,list.state as liststate
            ,list.yunhour as listyunhour,list.yununit as listyununit,list.tuidt as listtuidt')
            ->join('device_jin jin','jin.id=list.jinid')
            ->leftJoin('flow_bill bill','bill.id=jin.billid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_jin', 'jin'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_device_jin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','in',[0,1]];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jin.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jin.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = DeviceJinModel::alias('jin')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.number as cainumber,cai.gonger,bill.sericnum,bill.modename')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->leftJoin('flow_bill bill','bill.id=jin.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_jin', 'cai'))
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_jin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_device_jin(Request $request){
        $param = $request->post();
        $data = DeviceJinModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DeviceJinModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'device_jin', $data['id'], '进场登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_device_jin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DeviceJinModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($data['data']['caiid']);


        $list = DeviceJinListModel::where(['jinid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('device_jin', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_device_yun(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['yun.deleted_user','=', 0];
        $where[] = ['yun.comid','=',$this->userinfo['comid']];
        $where[] = ['yun.isturn','=',1];
        $where[] = ['yun.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['yun.enginid','=', $param['enginid']];
        }
        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.listid','=', $param['listid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['yun.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['ming.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['ming.shenum','like', '%'.$param['listshenum'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['yun.status','=', $param['status']];
        }


        $data['data'] = DeviceYunListModel::alias('list')
            ->field('yun.*,e.name as enginname,bill.sericnum,cai.name as cainame,cai.gonger
            ,list.qidt,list.zhidt,list.yunhour,list.yununit,list.explain as listexplain
            ,ming.name as listname,ming.shenum as listshenum,ming.unit as listunit,ming.price as listprice
            ,ming.jindt as listjindt')
            ->leftJoin('device_jin_list ming','ming.id=list.listid')
            ->join('device_jin jin','jin.id=ming.jinid')
            ->join('device_yun yun','yun.id=list.yunid')
            ->leftJoin('flow_bill bill','bill.id=yun.billid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_yun', 'yun'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_device_yun(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['yun.deleted_user','=', 0];
        $where[] = ['yun.comid','=',$this->userinfo['comid']];
        $where[] = ['yun.isturn','=',1];
        $where[] = ['yun.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['yun.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['yun.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['yun.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['yun.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['yun.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['yun.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = DeviceYunModel::alias('yun')
            ->field('yun.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=yun.enginid')
            ->leftJoin('flow_bill bill','bill.id=yun.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_yun', 'cai'))
            ->order('yun.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_yun');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_device_yun(Request $request){
        $param = $request->post();
        $data = DeviceYunModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DeviceYunModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'device_yun', $data['id'], '设备运行登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_device_yun(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DeviceYunModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $list = DeviceYunListModel::alias('list')
            ->field('list.*,ming.name,ming.unit,ming.price,ming.shenum,ming.jindt
                ,cai.name as cainame,cai.gonger')
            ->leftJoin('device_jin_list ming','ming.id=list.listid')
            ->leftJoin('device_jin jin','jin.id=ming.jinid')
            ->leftJoin('contract_cai cai','cai.id=jin.caiid')
            ->where(['list.yunid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('device_yun', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_device_tui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['tui.deleted_user','=', 0];
        $where[] = ['tui.comid','=',$this->userinfo['comid']];
        $where[] = ['tui.isturn','=',1];
        $where[] = ['tui.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['tui.enginid','=', $param['enginid']];
        }
        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.listid','=', $param['listid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['ming.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['ming.shenum','like', '%'.$param['listshenum'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['tui.status','=', $param['status']];
        }


        $data['data'] = DeviceTuiListModel::alias('list')
            ->field('tui.*,e.name as enginname,bill.sericnum,cai.name as cainame,cai.gonger
            ,list.tuidt,list.explain as listexplain
            ,ming.name as listname,ming.shenum as listshenum,ming.unit as listunit,ming.price as listprice
            ,ming.jindt as listjindt')
            ->leftJoin('device_jin_list ming','ming.id=list.listid')
            ->join('device_jin jin','jin.id=ming.jinid')
            ->join('device_tui tui','tui.id=list.tuiid')
            ->leftJoin('flow_bill bill','bill.id=tui.billid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_tui', 'tui'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_device_tui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['tui.deleted_user','=', 0];
        $where[] = ['tui.comid','=',$this->userinfo['comid']];
        $where[] = ['tui.isturn','=',1];
        $where[] = ['tui.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['tui.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['tui.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['tui.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tui.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['tui.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = DeviceTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=tui.enginid')
            ->leftJoin('flow_bill bill','bill.id=tui.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_tui', 'cai'))
            ->order('tui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_tui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_device_tui(Request $request){
        $param = $request->post();
        $data = DeviceTuiListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DeviceTuiListModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'device_tui', $data['id'], '设备退场登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_device_tui(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DeviceTuiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $list = DeviceTuiListModel::alias('list')
            ->field('list.*,ming.name,ming.unit,ming.price,ming.shenum,ming.jindt
                ,cai.name as cainame,cai.gonger')
            ->leftJoin('device_jin_list ming','ming.id=list.listid')
            ->leftJoin('device_jin jin','jin.id=ming.jinid')
            ->leftJoin('contract_cai cai','cai.id=jin.caiid')
            ->where(['list.tuiid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('device_tui', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }






}
